package com.example.imagepicker.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.example.imagepicker.model.Photo;
import com.example.imagepicker.utils.SDCardImageLoader;
import com.example.imagepicker.utils.ScreenUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tool.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页面中GridView的适配器
 * 
 * @author hanj
 */

public class MainGVAdapter extends BaseAdapter {

    private Context context;
    private List<Photo> imagePathList = new ArrayList<Photo>();

    private SDCardImageLoader loader;

    public MainGVAdapter(Context context, List<Photo> imagePathList) {
        this.context = context;
        this.imagePathList = imagePathList;

        loader = new SDCardImageLoader(ScreenUtils.getScreenW(), ScreenUtils.getScreenH());
    }

    @Override
    public int getCount() {
        return imagePathList == null ? 0 : imagePathList.size();
    }

    @Override
    public Object getItem(int position) {
        return imagePathList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Photo photo = (Photo) getItem(position);

        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.main_gridview_item, null);

            holder.imageView = (ImageView) convertView.findViewById(R.id.main_gridView_item_photo);
            holder.iv_status = (ImageView) convertView.findViewById(R.id.iv_status);
            holder.progressbar = (ProgressBar) convertView.findViewById(R.id.load_progressBar);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position == imagePathList.size() - 1) {
            holder.imageView.setTag("");
            String url = "assets://icon_addpic_unfocused.png";
            ImageLoader.getInstance().displayImage(url, holder.imageView);
            holder.imageView.setColorFilter(null);
            holder.iv_status.setVisibility(View.GONE);
            holder.progressbar.setVisibility(View.GONE);

        } else {
            holder.imageView.setTag(photo.getImgPath());
            loader.loadImage(3, photo.getImgPath(), holder.imageView);

            if (photo.getStatus() == 1) {// 待上传
                holder.imageView.setColorFilter(context.getResources().getColor(R.color.image_checked_bg2));
                holder.iv_status.setVisibility(View.GONE);
                holder.progressbar.setVisibility(View.VISIBLE);
            } else if (photo.getStatus() == 2) {// 上传成功
                holder.imageView.setColorFilter(null);
                holder.iv_status.setVisibility(View.VISIBLE);
                holder.progressbar.setVisibility(View.GONE);
                holder.iv_status.setImageDrawable(context.getResources().getDrawable(R.drawable.scan_item_normal));
            } else if (photo.getStatus() == 3) {// 上传失败
                holder.imageView.setColorFilter(null);
                holder.iv_status.setVisibility(View.VISIBLE);
                holder.progressbar.setVisibility(View.GONE);
                holder.iv_status.setImageDrawable(context.getResources().getDrawable(R.drawable.scan_item_error));
            } else if (photo.getStatus() == 4) {// 暂不上传
                holder.imageView.setColorFilter(null);
                holder.iv_status.setVisibility(View.VISIBLE);
                holder.progressbar.setVisibility(View.GONE);
                holder.iv_status.setImageDrawable(context.getResources().getDrawable(R.drawable.scan_item_delete));
            } else {
                holder.progressbar.setVisibility(View.GONE);
                holder.imageView.setColorFilter(null);
                holder.iv_status.setVisibility(View.GONE);
            }

        }

        return convertView;
    }

    private class ViewHolder {

        ImageView imageView;
        ImageView iv_status;
        ProgressBar progressbar;
    }

}
